package com.yanqu.road.server.cmd.activity.fanxiuspring;

import com.yanqu.road.entity.activity.fanxiuspring.FanXiuSpringConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FanxiuspringProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fanxiuspring.FanXiuSpringModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fanxiuspring.FanXiuSpringMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@CmdApi(reqMsg = FanxiuspringProto.FanXiuSpringDrawReqMsg.class, respMsg = FanxiuspringProto.FanXiuSpringDrawRespMsg.class)
@Cmd(code = Protocol.S_FAN_XIU_SPRING_DRAW, desc = "宝阁取宝")
public class FanXiuSpringDrawCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        FanXiuSpringConfig config = FanXiuSpringMgr.getActivityConfig(FanXiuSpringMgr.CHILD_TYPE_OTHER);
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        FanxiuspringProto.FanXiuSpringDrawReqMsg reqMsg = FanxiuspringProto.FanXiuSpringDrawReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.getType() != 0 && reqMsg.getType() != 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        ret = player.getModule(FanXiuSpringModule.class).draw(reqMsg.getType());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_FAN_XIU_SPRING_DRAW, FanxiuspringProto.FanXiuSpringDrawRespMsg.newBuilder().setRet(ret));
        }
    }
}
